package halk;

import com.alibaba.fastjson.JSON;
import entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Date;

/**
 * es test
 *
 * @Author halk
 * @Date 2023/2/24 10:01
 **/
public class ESHignLevelClientDemo {

    private RestHighLevelClient esClient;

    @BeforeEach
    public void before() {
        esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
    }

    @AfterEach
    public void after() throws IOException {
        esClient.close();
    }


    @Test
    public void createIndex() throws IOException {
        CreateIndexResponse createIndexResponse = esClient.indices().create(new CreateIndexRequest("user"), RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.isAcknowledged());
    }

    @Test
    public void queryIndex() throws IOException {
        GetIndexResponse getIndexResponse = esClient.indices().get(new GetIndexRequest("user"), RequestOptions.DEFAULT);
        System.out.println(getIndexResponse.getAliases());
        System.out.println(getIndexResponse.getMappings());
        System.out.println(getIndexResponse.getSettings());
    }

    @Test
    public void deleteIndex() throws IOException {
        AcknowledgedResponse acknowledgedResponse = esClient.indices().delete(new DeleteIndexRequest("user"), RequestOptions.DEFAULT);
        System.out.println(acknowledgedResponse.isAcknowledged());
    }

    @Test
    public void insert() throws IOException {
        IndexRequest indexRequest = new IndexRequest();
        indexRequest.index("user").id("1001");

        User user = new User().setName("xiaoming").setAge(12).setCreateTime(new Date());
        String userJson = JSON.toJSONString(user);
        indexRequest.source(userJson, XContentType.JSON);

        IndexResponse indexResponse = esClient.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse.getResult());
        System.out.println(indexResponse.getId());
        System.out.println(indexResponse.getIndex());
    }

    @Test
    public void getDocument() throws IOException {
        GetRequest getRequest = new GetRequest("user", "1001");
        GetResponse documentFields = esClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(documentFields);
        System.out.println(documentFields.getSourceAsString());
    }

    /**
     * 批量插入
     *
     * @throws IOException
     */
    @Test
    public void bulkInsert() throws IOException {

        BulkRequest bulkRequest = new BulkRequest("user");
        for (int i = 0; i < 10; i++) {
            User user = new User().setAge(10 + i).setCreateTime(new Date()).setName("xiao ming" + i);
            IndexRequest indexRequest = new IndexRequest().index("user").id(Integer.toString(200 + i)).source(JSON.toJSONString(user), XContentType.JSON);

            bulkRequest.add(indexRequest);
        }

        esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

    @Test
    public void search() throws IOException {
        SearchRequest searchRequest = new SearchRequest("user");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("name", "xiao"));
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
    }


}
